# 生成演示数据


import django
django.setup()

from serverworker import views
import sys
from serverworker.models import testmod, Device_info, Device_attribute,Device_maintenance_log
from serverworker.models import organ_index_company,organ_index_scene,organ_index_class,organ_index_project
import os
import random
import uuid
import datetime
import random

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'matrix.settings')  # 添加了环境

company=["万御安防"]
scene=["数据中心","办公楼","停车场"]
project=["背景音乐","周界安防","照明系统","活动感知","门禁","监控","上网"]
device_name=["注塑机","挤出机","热压机","冲床","剪切机","折弯机","冷镦机","钻床","磨床","铣床","车床","拉床","锻压机","成型机","压铸机"]
create_user=[1,2,3]
status=[1]
user = [1,2,3]


def fun_select_device_info():
    value = Device_info.objects.all()
    lis_value =value.values()
    print(value.values())
    return lis_value
def fun_select_organ_index():
    lis_organ_index_company = organ_index_company.objects.all().values()
    lis_organ_index_scene =organ_index_scene.objects.all().values()
    lis_organ_index_class =organ_index_class.objects.all().values()
    lis_organ_index_project =organ_index_project.objects.all().values()
    print(lis_organ_index_company)


def fun_insert_device_info():
    #Device_info.objects.all().delete()


    def random_chinese(length):
        res = ''
        for _ in range(length):
            head = random.randint(0xb0, 0xf7)
            body = random.randint(0xa1, 0xfe)
            val = f'{head:x}{body:x}'
            res += (bytes.fromhex(val).decode('gb2312'))
        return res

    for i in range(0,20):
        fun = Device_info.objects.create(
            company_id=random.choice(list(organ_index_company.objects.values('id')))['id'],
            scene_id=random.choice(list(organ_index_scene.objects.values('id')))['id'],
            project_id=random.choice(list(organ_index_project.objects.values('id')))['id'],
            device_class_id= random.choice(list(organ_index_class.objects.values('id')))['id'],

            device_name=random.choice(device_name)+"-"+str(random.choice(['A','B','C','D',1,2,3])),
            device_id=uuid.uuid4().hex[16:],
            admin_user_id=random.choice(user),
            create_user_id=random.choice(user),
            watch_user_id=random.choice(user),
            teach_user_id=random.choice(user),
            func=random_chinese(10),
            status=1,
        )
        print("当前行号：",sys._getframe().f_lineno,fun.device_name,fun.func)

def fun_insert_Device_attribute():

    def random_chinese(length):
        res = ''
        for _ in range(length):
            head = random.randint(0xb0, 0xf7)
            body = random.randint(0xa1, 0xfe)
            val = f'{head:x}{body:x}'
            res += (bytes.fromhex(val).decode('gb2312'))
        return res
    lis = fun_select_device_info()

    #表头
    for i in lis:
        print(i['device_id'])
        fun = Device_attribute.objects.create(
            device_name_id=i['device_id'],
            a1='端口',
            b1='光/电',
            c1='连接',
            d1='功能',
            e1='',
            f1='',
            g1='',
            h1='',
            notes='',
            create_user_id=random.choice(user),
            order_id=0, #random.choice(range(0,100)),
            is_head=1,
            status=1,
        )
        print("当前行号：",sys._getframe().f_lineno,fun.device_name_id)

    # 配置信息
    for i in lis:
        for j in [random.randint(0, 100) for _ in range(random.randint(3, 7))]:
            fun = Device_attribute.objects.create(
                device_name_id=i['device_id'],
                a1='01',
                b1=random.choice(['光','电']),
                c1='相机'+str(random.choice(range(0,100))),
                d1=random_chinese(6),
                e1='',
                f1='',
                g1='',
                h1='',
                notes='123',
                create_user_id=random.choice(user),
                order_id=random.choice(range(0,100)),
                is_head=0,
                status=1,
            )
            print("当前行号：", sys._getframe().f_lineno, fun.device_name_id)


def fun_insert_maintenance_log():
    li_q  =['宇视监控B区-2层摄像头掉线','宇视监控-2B002摄像头掉线','宇视监控-2B021摄像头掉线','宇视监控-1B019摄像头位置异常','宇视监控-1C008摄像头位置异常','宇视监控18c005摄像头损坏','宇视监控-2b028摄像头掉线','宇视监控-2c022摄像头无图像','宇视监控-3c021摄像头无图像','金三立系统故障告警','A货梯监控位置异常','407会议室功放无声音','C区18楼ups电池损坏','A区18楼弱电间门禁损坏','宇视监控12A005录像丢失','宇视监控9A003录像丢失','宇视监控17B003录像丢失','BA系统负一楼掉线','A区1楼UPS电池损坏','监控室的捷顺系统登录失败','宇视监控D区电梯监控看不清','金三立监控摄像头时间显示不全','C区一台闸机门抖动','BA系统负一楼掉线','宇视监控B区-1楼监控掉线','宇视监控D区1楼监控掉线','宇视监控A区-1楼监控掉线','宇视监控A区-2楼监控掉线']
    li_a  =['更换设备','重启','重启','手动调整','手动调整','更换设备','更换设备','重启','重启','根据故障清理','手动调整','重新设置','更换电池','更换','更换硬盘','更换硬盘','更换硬盘','重启','更换电池','手动配置','更换','手动调整','手动调整','重启','恢复供电','恢复供电','恢复供电','重启交换机']
    lis = fun_select_device_info()
    for i in lis:
        if 1:
            fun = Device_maintenance_log.objects.create(
                device_name_id= i['device_id'],
                maintenance_user_id= random.choice(user),
                replace= random.choice([0,1]),
                problem= random.choice(li_q),
                solution= random.choice(li_a),
                notes= '123',
                photo1= '/cover_img/default.jpg',
                photo2= '/cover_img/default.jpg',
                photo3= '/cover_img/default.jpg',
                plan=random.choice(['等待处理','正在进行', '维修完成']),
                status= True,
            )
            print("当前行号：", sys._getframe().f_lineno, fun.device_name_id)

#公司项目现场分类
#organ_index_company,organ_index_scene,organ_index_class,organ_index_project
def organ_index():

    lis_comp  = ['万御安防','沃尔玛','亚马逊','国家电网有限公司','沙特阿美公司']
    for i in lis_comp:
        fun = organ_index_company.objects.create(
            company=i,
            comp_addr='成都市武侯区天府大道108号',
            main_user1='李白/17898768800',
            main_user2='李白/17898768800',
            create_user_id= random.choice(user),
            status= True,
        )
        print("当前行号：", sys._getframe().f_lineno, fun.company)
    lis_scene  = ['主楼','A栋','2栋','16栋']
    for i in lis_scene:
        fun = organ_index_scene.objects.create(
            scene=i,
            func = '这是相关说明',
            create_user_id= random.choice(user),
            status= True,
        )
        print("当前行号：", sys._getframe().f_lineno, fun.scene)

    lis_project =["背景音乐","周界安防","照明系统","活动感知","门禁","监控","上网"]
    for i in lis_project:
        fun = organ_index_project.objects.create(
            project=i,
            func = '这是相关说明',
            create_user_id= random.choice(user),
            status= True,
        )
        print("当前行号：", sys._getframe().f_lineno, fun.project)

    lis_class  =['照明设备','动力设备','电热设备','控制设备','0类设备','I类设备','Ⅱ类设备','Ⅲ类设备','发电机','电动机','变压器','高压断路器','隔离开关','负荷开关','熔断器','电抗器','避雷器','母线','电缆','互感器等']
    for i in lis_class:
        fun = organ_index_class.objects.create(
            device_class=i,
            func = '这是相关说明',
            create_user_id= random.choice(user),
            status= True,
        )
        print("当前行号：", sys._getframe().f_lineno, fun.device_class)


if __name__ == '__main__':
    print('1')

    #fun_insert_device_info()


    #fun_insert_Device_attribute()


    fun_insert_maintenance_log()
    #organ_index()
